package hongcaosp.app.android.user.settings.wallet.presenter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.ArrayList;
import java.util.List;

import hongcaosp.app.android.R;
import hongcaosp.app.android.common.BaseAdapter;
import hongcaosp.app.android.common.BaseHolder;
import hongcaosp.app.android.login.UserToken;
import hongcaosp.app.android.modle.BaseResponse;
import hongcaosp.app.android.modle.DataCallBack;
import hongcaosp.app.android.modle.bean.ChargeMoney;
import hongcaosp.app.android.modle.bean.ChargeMoneyWrap;
import hongcaosp.app.android.modle.bean.PayOrder;
import hongcaosp.app.android.modle.bean.UserWallet;
import hongcaosp.app.android.modle.bean.UserWalletWrap;
import hongcaosp.app.android.modle.impl.UserWalletModelImpl;
import hongcaosp.app.android.user.settings.wallet.dialog.DialogPayType;
import hongcaosp.app.android.user.settings.wallet.holder.ChargeMoneyHolder;
import hongcaosp.app.android.user.settings.wallet.iview.UserWalletIView;
import xlj.lib.android.base.component.BasePresenter;
import xlj.lib.android.base.toastcompat.ToastManager;
import xlj.lib.android.base.view.OnRecycleItemClickListener;

/**
 * 钱包
 */
public class WalletPresenter extends BasePresenter {

    private Context context;
    private UserWalletIView iView;
    private UserWalletModelImpl userWalletModel;

    //支付方式
    private DialogPayType dialogPayType;

    private BaseAdapter<ChargeMoney> moneyAdapter;
    private long chargeMoneyId = -1;
    private List<ChargeMoney> moneyList = new ArrayList<>();

    private int type = -1;  //0-支付宝 1-微信
    private int operationType = 2;//1-开通会员 2-充值
    private UserWallet userWallet;

    public WalletPresenter(Context context, UserWalletIView iView) {
        this.context = context;
        this.iView = iView;
        this.userWalletModel = new UserWalletModelImpl();
    }

    public void loadInfo() {
        userWalletModel.userWallet(UserToken.getDefault().getToken(), new DataCallBack<UserWalletWrap>() {
            @Override
            public void onGetData(UserWalletWrap data) {
                super.onGetData(data);
                if (data != null && data.getUserWallet() != null) {
                    iView.walletInfo(userWallet = data.getUserWallet());
                }
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                ToastManager.getInstance().showToast(errorMessage);
            }
        });

        userWalletModel.rechargeList(UserToken.getDefault().getToken(), 2, new DataCallBack<ChargeMoneyWrap>() {
            @Override
            public void onGetData(ChargeMoneyWrap data) {
                super.onGetData(data);
                moneyList.clear();
                if (data != null) {
                    moneyList.addAll(data.getList());
                }
                if (moneyList.size() > 0) {
                    moneyList.get(0).setSelect(true);
                    chargeMoneyId = moneyList.get(0).getId();
                    iView.selectMoneyItem(moneyList.get(0).getPayPrice());
                }
                moneyAdapter.setData(moneyList);
                moneyAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                ToastManager.getInstance().showToast(errorMessage);
            }
        });
    }

    /**
     * 绑定提现账号
     * @param type
     * @param account
     */
    public void bindingPayType(int type, String account) {
        userWalletModel.bindingPayType(UserToken.getDefault().getToken(), type, account, new DataCallBack<BaseResponse>() {
            @Override
            public void onGetData(BaseResponse data) {
                super.onGetData(data);
                iView.cashWithdrawalRst(data);
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                ToastManager.getInstance().showToast(errorMessage);
            }
        });
    }

    public void initChargeMoney(RecyclerView rv) {
        moneyAdapter = new BaseAdapter<ChargeMoney>() {
            @NonNull
            @Override
            public BaseHolder<ChargeMoney> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new ChargeMoneyHolder(LayoutInflater.from(context).inflate(R.layout.item_wallet_charge_money,parent,false), new OnRecycleItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        for(ChargeMoney item : moneyList) {
                            item.setSelect(false);
                        }
                        moneyList.get(position).setSelect(true);
                        moneyAdapter.notifyDataSetChanged();

                        chargeMoneyId = moneyList.get(position).getId();
                        iView.selectMoneyItem(moneyList.get(position).getPayPrice());
                    }

                    @Override
                    public void onItemLongClick(int position) {

                    }
                });
            }
        };
        moneyAdapter.addData(moneyList);
        GridLayoutManager flm = new GridLayoutManager(context,3,GridLayoutManager.VERTICAL,false);
        rv.setLayoutManager(flm);
        rv.setAdapter(moneyAdapter);
    }

    public void pay() {
        if (dialogPayType == null) {
            dialogPayType = new DialogPayType(context, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialogPayType.dismiss();
                    switch (view.getId()) {
                        case R.id.ll_wechat:
                            //微信支付
                            type = 1;
                            wx_pay();
                            break;
                        case R.id.ll_alipay:
                            //支付宝支付
                            type = 0;
                            break;
                    }
                }
            });
        }
        dialogPayType.show();
    }

    private void wx_pay() {
        userWalletModel.openMember(UserToken.getDefault().getToken(), type, operationType, chargeMoneyId, new DataCallBack<PayOrder>() {
            @Override
            public void onGetData(PayOrder data) {
                super.onGetData(data);
                if (data != null) {
                    PayReq payReq = new PayReq();
                    payReq.appId = data.getAppid();
                    payReq.partnerId = data.getPartnerid();
                    payReq.prepayId= data.getPrepayId();
                    payReq.packageValue = "Sign=WXPay";
                    payReq.nonceStr= data.getNonceStr();
                    payReq.timeStamp= data.getTimestamp();
                    payReq.sign= data.getSign();

                    IWXAPI iwxapi = WXAPIFactory.createWXAPI(context,data.getAppid());
                    iwxapi.sendReq(payReq);
                }
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                ToastManager.getInstance().showToast(errorMessage);
            }
        });
    }

    public UserWallet getUserWallet() {
        return userWallet;
    }

    public boolean isBindWXAccount() {
        if (userWallet == null || userWallet.getBindWx() == null) {
            return false;
        }
        return userWallet.getBindWx();
    }

    public boolean isBindALiAccount() {
        if (userWallet == null || userWallet.getBindAli() == null) {
            return false;
        }
        return userWallet.getBindAli();
    }

    public Double getReadAmount() {
        if (userWallet == null || userWallet.getRedAmount() == null) {
            return 0.0;
        }
        return userWallet.getRedAmount();
    }
}
